package cn.evilelf.sort;

public class Insertion {
	public static int[] sort(int array[]) {
		for (int i = 1; i < array.length; ++i) {
			int tmp = array[i];
			int j;
			for (j = i; j > 0 && tmp < array[j - 1]; --j) {
				array[j] = array[j - 1];
			}
			array[j] = tmp;
		}

		return array;
	}

	public static long[] sort(long array[]) {
		for (int i = 1; i < array.length; ++i) {
			long tmp = array[i];
			int j;
			for (j = i; j > 0 && tmp < array[j - 1]; --j) {
				array[j] = array[j - 1];
			}
			array[j] = tmp;
		}

		return array;
	}

	public static float[] sort(float array[]) {
		for (int i = 1; i < array.length; ++i) {
			float tmp = array[i];
			int j;
			for (j = i; j > 0 && tmp < array[j - 1]; --j) {
				array[j] = array[j - 1];
			}
			array[j] = tmp;
		}

		return array;
	}

	public static double[] sort(double array[]) {
		for (int i = 1; i < array.length; ++i) {
			double tmp = array[i];
			int j;
			for (j = i; j > 0 && tmp < array[j - 1]; --j) {
				array[j] = array[j - 1];
			}
			array[j] = tmp;
		}

		return array;
	}

	@SuppressWarnings("unchecked")
	public static Comparable[] sort(Comparable[] array) {
		for (int i = 1; i < array.length; ++i) {
			Comparable tmp = array[i];
			int j;
			for (j = i; j > 0 && tmp.compareTo(array[j - 1]) < 0; --j) {
				array[j] = array[j - 1];
			}
			array[j] = tmp;
		}

		return array;
	}

	public static void main(String[] args) {
		String[] array = { "bad", "mum", "fuck", "love", "chuanshiji", "from" };
		sort(array);
		for (int i = 0; i < array.length; ++i) {
			System.out.println(array[i]);
		}
	}
}
